﻿Imports System.ComponentModel
Imports Excepciones

Public Class NumericUpDownExt
    Inherits NumericUpDown
    Implements IControlValidatable

#Region "Validacion"


    ''' <summary>
    ''' Se produce cuando el control de esta validando, pero antes de la validación por tipo
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ValidatingBefore(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

    ''' <summary>
    ''' Se produce cuando el control de esta validando, pero luego de la validación por tipo
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ValidatingAfter(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

    Private _validations As New List(Of Utilidades.ValidationType)
    <Browsable(True), _
EditorBrowsable(EditorBrowsableState.Always), _
Category("Validation"), _
Description("Valida el texto con las opciones seleccionadas"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public Property Validation() As List(Of Utilidades.ValidationType)
        Get
            Return _validations
        End Get
        Set(ByVal value As List(Of Utilidades.ValidationType))
            _validations = value
        End Set
    End Property

    Public Sub ValidateInput() Implements IControlValidatable.ValidateInput

        Dim text As String = Convert.ToString(Me.Value)
        If Me.Value = 0 Then
            text = ""
        End If
        For Each tipo As Utilidades.ValidationType In _validations
            Utilidades.Validations.Validate(tipo, text)
        Next

    End Sub

    Private Sub Me_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
        Dim frm As Base
        Try
            frm = DirectCast(Me.FindForm(), Base)
            frm.ResetErrorUI(sender)
            Try
                RaiseEvent ValidatingBefore(sender, e)
                ValidateInput()
                RaiseEvent ValidatingAfter(sender, e)
            Catch ex As CustomException
                e.Cancel = True
                frm.SetErrorUI(sender, ex)
            End Try
        Catch ex As Exception

        End Try

    End Sub
#End Region

End Class
